﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;

using SpeakupBackend;
using SpeakupBackend.Classes;

namespace Frontend
{
    /// <summary>
    /// Interaction logic for ClassInfoPanel.xaml
    /// </summary>
    public partial class ClassInfoPanel : UserControl
    {
        public ClassInfoPanel()
        {
            InitializeComponent();

            Update(null);
        }

        public void Update(Class speakupClass)
        {
            Panel.Children.Clear();

            if (speakupClass == null)
            {
                Panel.Background = Brushes.LightYellow;

                TextBlock box = new TextBlock(new Bold(new Run("Select or hover over any class " +
                                                                "for more details about it.")));
                box.HorizontalAlignment = HorizontalAlignment.Center;
                box.VerticalAlignment = VerticalAlignment.Center;
                box.TextAlignment = TextAlignment.Center;
                box.TextWrapping = TextWrapping.Wrap;
                box.Padding = new Thickness(10);
                Panel.Children.Add(box);
                return;
            }

            UpdateStatus(speakupClass);

            Panel.Background = App.GetClassBrush(speakupClass.DesiredStatus);

            TextBlock title = new TextBlock();
            title.HorizontalAlignment = HorizontalAlignment.Center;
            title.VerticalAlignment = VerticalAlignment.Center;
            title.TextAlignment = TextAlignment.Center;

            //name
            {
                Run className = new Run(speakupClass.Name.ToString());
                title.Inlines.Add(new Bold(className));
            }

            title.Inlines.Add(new Run("\n"));

            //theme
            if(speakupClass.Theme != null)
            {
                
                Run classTheme = new Run(speakupClass.Theme.Name);

                if (speakupClass.Theme.IsAttended)
                {
                    classTheme.TextDecorations = TextDecorations.Strikethrough;
                }
                
                classTheme.Foreground = new SolidColorBrush(Color.FromArgb(200, 20, 20, 20));
                title.Inlines.Add(new Italic(new Bold(classTheme)));

                title.Inlines.Add(new Run("\n"));
            }           

            //time
            {
                TimeSlot timeSlot = new TimeSlot(speakupClass.Time.Begin, speakupClass.Time.End);
                Run classTime = new Run(timeSlot.ToString());
                title.Inlines.Add(classTime);
            }

            title.Inlines.Add(new Run("\n"));

            //date
            {
                DateTime date = speakupClass.Time.Date;
                string strDate = date.ToString("dddd\ndd MMMM", CultureInfo.InvariantCulture);
                Run classDate = new Run(strDate);
                title.Inlines.Add(classDate);
            }

            title.Inlines.Add(new Run("\n"));

            //date hint
            {
                DateTime classDate = speakupClass.Time.Date;
                DateTime now = DateTime.Now.Date;

                TimeSpan delta = classDate - now;

                string weekHint = null;
                if (delta.TotalDays <= 7 &&
                    classDate.DayOfWeek >= now.DayOfWeek)
                {
                    weekHint = "this week";
                }
                else if (delta.TotalDays <= 7)
                {
                    weekHint = "next week";
                }
                else if (delta.TotalDays <= 7 &&
                    classDate.DayOfWeek > now.DayOfWeek)
                {
                    weekHint = "next week";
                }

                int days = (int)delta.TotalDays;

                string dayHint = days.ToString() + " day" + ((days > 1) ? "s" : "") + " left";
                string hint = dayHint;

                if (days == 0)
                {
                    hint = "today";
                }
                else if (days == 1)
                {
                    hint = "tomorrow";
                }
                else if (weekHint != null)
                {
                    hint = weekHint + ", " + hint;
                }

                hint = "(" + hint + ")";

                Italic italic = new Italic(new Run(hint));
                italic.FontSize = 11;
                title.Inlines.Add(italic);
            }

            title.Inlines.Add(new Run("\n"));
            title.Inlines.Add(new Run("\n"));

            //id
            {

                string id = speakupClass.Id.ToString();

                Inline idInline = new Italic(new Run(id));
                idInline.FontSize = 10;
                    
                title.Inlines.Add(idInline);
            }

            

            Panel.Children.Add(title);
        }

        void UpdateStatus(Class speakupClass)
        {
            string message = null;            
            Brush background = null;

            switch (speakupClass.DesiredStatus)
            {
                case ClassStatus.Available:
                    message = "Available.\nYou can book this class.";
                    background = Brushes.Yellow;
                    break;

                case ClassStatus.Reserved:
                    message = "Reserved by you.\nYou can cancel reservation.";
                    background = Brushes.Pink;
                    break;

                case ClassStatus.AvailableWaitList:
                    message = "Availble for Wait List only.\nYou can sign up for wait list.";
                    background = Brushes.Orange;
                    break;
                    
                case ClassStatus.ReservedWaitList:
                    message = "You are on wait list.\nYou can cancel reservation.";
                    background = Brushes.Pink;
                    break;
            }

            if (message != null)
            {
                TextBlock block = new TextBlock();
                block.FontSize = 12;
                block.HorizontalAlignment = HorizontalAlignment.Center;
                block.VerticalAlignment = VerticalAlignment.Center;
                block.TextAlignment = TextAlignment.Center;

                block.Inlines.Add(new Italic(new Run(message)));

                Border border = new Border();
                border.Background = background;
                border.BorderThickness = new Thickness(0, 0, 0, 1);
                border.BorderBrush = Brushes.Black;
                border.Child = block;

                DockPanel.SetDock(border, Dock.Top);
                Panel.Children.Add(border);                
            }

        }
    }
}
